package com.topsun.posclient.common.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**                                                    
 * 功能描述: 添加说明                                  
 * Copyright: Copyright (c) 2011                       
 * Company: Tango               
 * @author  TanDong                    
 * @version 1.0 2011-8-25上午09:48:21                         
 * @see                                                
 *	HISTORY                                            
 * 2011-8-25上午09:48:21 创建文件                             
 **************************************************/
@SuppressWarnings("rawtypes")
public class QueryConditions implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
     * 等于
     */
    public static final String EQUAL = "=";
    /**
     * 不等�?
     */
    public static final String NOT_EQUAL = "<>";
    /**
     * 大于
     */
    public static final String GREATER_THAN = ">";
    /**
     * 大于等于
     */
    public static final String GREATER_EQUAL = ">=";
    /**
     * 小于
     */
    public static final String LESS_THAN = "<";
    /**
     * 小于等于
     */
    public static final String LESS_EQUAL = "<=";
    /**
     * 中间包含
     */
    public static final String INCLUDE = "%value%";
    /**
     * 左边包括
     */
    public static final String LEFT_INCLUDE = "value%";
    /**
     * 右边包括
     */
    public static final String RIGHT_INCLUDE = "%value";
    /**
     * 空�?
     */
    public static final String ISNULL = "isnull";

    /**
     * 不为空�?
     */
    public static final String ISNOTNULL = "isnotnull";

    /**
     * 空字符串�?
     */
    public static final String ISEMPTY = "isempty";

    /**
     * 不为空字符串�?
     */
    public static final String ISNOTEMPTY = "isnotempty";


    /**
     * 排序语句
     */
    private String orderBy;

    /**
     * 分组语句
     */
    private String groupBy;
    
    /**
     * 查询的HQL其它语句，可为having�?
     */
    private String otherHql;

    /**
     * 查询条件的属性名列表
     */
	private List propertyNames;

    /**
     * 查询条件的操作符列表，与属�?名列表一�?��应�?操作符包�?, >=, <=, <>, !=, like
     */
    private List operators;

    /**
     * 查询条件的�?列表，该列表应当与属性列表一�?���?
     */
    private List values;
    
    /**
     * 查询Sql Map语句id
     */
    private String statementId;

    /**
     * 统计总记数查询的Sql Map语句idSql
     */
    private String countStatementId;

    /**
     * 查询参数值对�?可为Javabean,Map�?
     */
    private Object paramValue;
    
    public QueryConditions() {
        orderBy = null;
        groupBy = null;
        propertyNames = new ArrayList();
        operators = new ArrayList();
        values = new ArrayList();
        otherHql = null;
        statementId = null;
        countStatementId = null;
        paramValue = null;
    }
    /**
     * 设置查询条件(where后面),该方法迭加多次调用�?
     * 示例如下<br>
     * <pre>
     * hqlWrapper.setCondition("codeType", HqlWrapper.EQUAL, codeType);
     * hqlWrapper.setCondition("codeTypeName", HqlWrapper.EQUAL, codeTypeName);
     * </pre>
     *
     * @param propertyName 查询条件的属性名
     * @param operator     查询条件的操作符
     * @param value        查询条件的�?
     */
    @SuppressWarnings("unchecked")
	public void setCondition(String propertyName, String operator, Object value) {

        if (propertyName == null || propertyName.trim().equals("")) return;
        if (operator == null || operator.trim().equals("")) {
            operator = EQUAL;
        }

        if (operator.equals(ISNULL) ||
                operator.equals(ISNOTNULL) ||
                operator.equals(ISEMPTY) ||
                operator.equals(ISNOTEMPTY)) {
            value = operator;
        }

        if (value == null) return;
        if (value instanceof String && value.toString().trim().equals("")) return;
        propertyNames.add(propertyName);

        if (operator.equals(INCLUDE)) {
            operators.add("like");
            values.add("%" + value + "%");
        } else if (operator.equals(LEFT_INCLUDE)) {
            operators.add("like");
            values.add(value + "%");
        } else if (operator.equals(RIGHT_INCLUDE)) {
            operators.add("like");
            values.add("%" + value);
        } else {
            operators.add(operator);
            values.add(value);
        }
    }

    public void setConditionEqual(String propertyName, Object value) {
        setCondition(propertyName, EQUAL, value);
    }

    public void setConditionNotEqual(String propertyName, Object value) {
        setCondition(propertyName, NOT_EQUAL, value);
    }

    public void setConditionGreaterEqual(String propertyName, Object value) {
        setCondition(propertyName, GREATER_EQUAL, value);
    }

    public void setConditionGreaterThan(String propertyName, Object value) {
        setCondition(propertyName, GREATER_THAN, value);
    }

    public void setConditionLessEqual(String propertyName, Object value) {
        setCondition(propertyName, LESS_EQUAL, value);
    }

    public void setConditionLessThan(String propertyName, Object value) {
        setCondition(propertyName, LESS_THAN, value);
    }

    public void setConditionLeftInclude(String propertyName, Object value) {
        setCondition(propertyName, LEFT_INCLUDE, value);
    }

    public void setConditionRightInclude(String propertyName, Object value) {
        setCondition(propertyName, RIGHT_INCLUDE, value);
    }

    public void setConditionInclude(String propertyName, Object value) {
        setCondition(propertyName, INCLUDE, value);
    }

    public void setConditionIsNull(String propertyName) {
        setCondition(propertyName, ISNULL, ISNULL);
    }

    public void setConditionIsNotNull(String propertyName) {
        setCondition(propertyName, ISNOTNULL, ISNOTNULL);
    }

    public void setConditionIsEmpty(String propertyName) {
        setCondition(propertyName, ISEMPTY, ISEMPTY);
    }

    public void setConditionIsNotEmpty(String propertyName) {
        setCondition(propertyName, ISNOTEMPTY, ISNOTEMPTY);
    }
    public String getOrderBy() {
        return orderBy;
    }

    /**
     * 设置排序条件�?br>
     * 示例�?br>
     * <p/>
     * hqlWrapper.setOrderBy("a desc,b asc"); -->order by a desc,b asc
     *
     * @param orderBy 排序字符�?
     */
    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getGroupBy() {
        return groupBy;
    }

    /**
     * 设置分组条件�?br>
     * 示例�?br>
     * <p/>
     * hqlWrapper.setGroupBy("a,b"); --> group by a,b
     *
     * @param groupBy 分组字符�?
     */
    public void setGroupBy(String groupBy) {
        this.groupBy = groupBy;
    }

    /**
     * 查询的HQL其它语句，如having�?
     *
     * @return String
     */
    public String getOtherHql() {
        return otherHql;
    }

    public void setOtherHql(String otherHql) {
        this.otherHql = otherHql;
    }

    /**
     * 查询条件的属性名列表
     *
     * @return Set
     */
    public List getPropertyNames() {
        return propertyNames;
    }

    public void setPropertyNames(List propertyNames) {
        this.propertyNames = propertyNames;
    }

    /**
     * 查询条件的操作符列表，与属�?名列表一�?��应�?操作符包�?, >=, <=, <>, !=, like
     *
     * @return Set
     */
    public List getOperators() {
        return operators;
    }

    public void setOperators(List operators) {
        this.operators = operators;
    }

    /**
     * 查询条件的�?列表，该列表应当与属性列表一�?���?
     *
     * @return Set
     */
    public List getValues() {
        return values;
    }

    public void setValues(List values) {
        this.values = values;
    }
    
    //=======================================================
    /**
     * 设置iBatis执行查询语句Sql Map的statementId及执行查询�?计数(如果�?��返回查询总记录数)<br>
     * <p/>
     * 示例如下<br>
     * <pre>
     * //�??用普通Javabean作为参数查询�?br>
     * IwbFrameCodeType codeTypeObj = new IwbFrameCodeType();
     * codeTypeObj.setCodeType("0010");
     * codeTypeObj.setCodeTypeName("机构类型");
     * <p/>
     * sqlWrapper.setParameter("codeType.searchCodeType","codeType.countCodeTypeCount",codeTypeObj);
     * <p/>
     * //二�?用Map作为参数查询�?br>
     * Map map = new HashMap();
     * map.put("codeType","0010");
     * map.put("codeTypeName","机构类型");
     * <p/>
     * sqlWrapper.setParameter("codeType.searchCodeType","codeType.countCodeTypeCount",map);
     * <p/>
     * //三�?配置SQL Map文件
     * &lt;sqlMap namespace="codeType"&gt;
     * <p/>
     * &lt;typeAlias alias="IwbFrameCodeType" type="com.longtop.intelliweb.sample.base.model.IwbFrameCodeType"/&gt;
     * <p/>
     * &lt;resultMap id="IwbFrameCodeTypeResult" class="com.longtop.intelliweb.sample.base.model.IwbFrameCodeType"&gt;
     * &lt;result column="code_type" property="codeType" jdbcType="VARCHAR"/&gt;
     * &lt;result column="code_type_name" property="codeTypeName" jdbcType="VARCHAR"/&gt;
     * &lt;/resultMap&gt;
     * <p/>
     * &lt;!--以下定义查询语句--&gt;
     * &lt;select id="searchCodeType" resultMap="IwbFrameCodeTypeResult"  parameterClass="IwbFrameCodeType"&gt;
     * select code_type,code_type_name from iwb_frame_code_type
     * where code_type =#codeType# and code_type_name = #codeTypeName#
     * &lt;/select&gt;
     * <p/>
     * &lt;!--以下定义返回总记录数的查询语�?-&gt;
     * &lt;select id="countCodeTypeCount" resultClass="int" parameterClass="IwbFrameCodeType"&gt;
     * select count(*) as result from iwb_frame_code_type
     * where code_type =#codeType# and code_type_name = #codeTypeName#
     * &lt;/select&gt;
     * <p/>
     * &lt;/sqlMap&gt;
     * </pre>
     *
     * @param statementId      调用iBatis的SqlMap文件的声明段名，规则名：SqlMap的namespace+"." + 该sqlMap文件某片段的id
     * @param countStatementId 统计总记数查询的Sql Map语句的声明段名，规则名：SqlMap的namespace+"." + 该sqlMap文件某片段的id
     * @param paramValue       查询条件的�?，可为JavaBean,Map对象�?
     */
    public void setParameter(String statementId, String countStatementId, Object paramValue) {
        this.statementId = statementId;
        this.countStatementId = countStatementId;
        this.paramValue = paramValue;
    }


    public String getStatementId() {
        return statementId;
    }

    public void setStatementId(String statementId) {
        this.statementId = statementId;
    }

    public String getCountStatementId() {
        return countStatementId;
    }

    public void setCountStatementId(String countStatementId) {
        this.countStatementId = countStatementId;
    }

    public Object getParamValue() {
        return paramValue;
    }

    public void setParamValue(Object paramValue) {
        this.paramValue = paramValue;
    }
}